package com.kfm.base.thread.day01;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Demo03 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<>(){

            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 0; i < 10; i++) {
                    sum += i;
                }
                return sum;
            }
        };

        FutureTask<Integer> futureTask = new FutureTask(callable);

        new FutureTask<>(new Runnable(){

            @Override
            public void run() {
                int sum = 0;
                for (int i = 0; i < 10; i++) {
                    sum += i;
                }
            }
        }, 10);

        Thread t1 = new Thread(futureTask);

        t1.start();


        // Stop 等待执行完毕后获取结果
        System.out.println(futureTask.get());
        System.out.println("main end");
    }
}
